package com.wwy.consumer;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DeliverCallback;
import com.wwy.config.RabbitMqUtils;
import com.wwy.mapper.StuMapper;
import com.wwy.mapper.TbOrderMapper;
import com.wwy.module.Stu;
import com.wwy.module.TbOrder;
import com.wwy.service.TbOrderService;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * @author 王伟羽
 * @date 2024/3/13 17:06
 */
@Component
public class PublishConsumerOne {

    @Resource
    private TbOrderService tbOrderService;

    private final static String PUBLISH_PRODUCER1 = "publish_name_one";
    private final static String PUBLISH_PRODUCER2 = "publish_name_two";

    //交换机名称
    private final static String EXCHANGE_NAME = "publish_exchange";

    @Bean
    public void publishGetInfo() {
        Connection connection = RabbitMqUtils.getConnection();
        if (Objects.nonNull(connection)) {
            try {
                Channel channel = connection.createChannel();
                //保证消费者 每次只消费一条消息
                channel.basicQos(1);

                // 第一步声明 队列
                channel.queueDeclare(PUBLISH_PRODUCER1,true,false,false,null);
                channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
                channel.queueBind(PUBLISH_PRODUCER1, EXCHANGE_NAME, "");
                DeliverCallback deliverCallback = (consumerTag, delivery) -> {
                    String message = new String(delivery.getBody(), "UTF-8");
                    tbOrderService.insertTbOrder(message);
                    System.out.println(" 队列一消费消息" + message);
                    channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
                };
                channel.basicConsume(PUBLISH_PRODUCER1, true, deliverCallback, consumerTag -> {
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Bean
    public void publishGetInfoTwo() {
        Connection connection = RabbitMqUtils.getConnection();
        if (Objects.nonNull(connection)) {
            try {
                Channel channel = connection.createChannel();
                //保证消费者 每次只消费一条消息
                channel.basicQos(1);
                channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
                channel.queueDeclare(PUBLISH_PRODUCER2,true,false,false,null);
                channel.queueBind(PUBLISH_PRODUCER2, EXCHANGE_NAME, "");
                DeliverCallback deliverCallback = (consumerTag, delivery) -> {
                    String message = new String(delivery.getBody(), "UTF-8");
                    System.out.println(" 队列二消费消息" + message);
                    channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
                };
                channel.basicConsume(PUBLISH_PRODUCER2, true, deliverCallback, consumerTag -> {
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
